package com.mlethe.library.socialtool.core;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

class Util {
    /**
     * Bitmap 格式化
     *
     * @param bitmap
     * @return
     */
    static Bitmap bitmapFormat(Bitmap bitmap) {
        if (bitmap == null) {
            return null;
        }
        int width = 300;
        int height = (int) (bitmap.getHeight() / (float) bitmap.getWidth() * width);
        return bitmapFormat(bitmap, width, height);
    }

    /**
     * Bitmap 格式化
     *
     * @param bitmap
     * @param width  宽度
     * @param height 高度
     * @return
     */
    static Bitmap bitmapFormat(Bitmap bitmap, int width, int height) {
        if (bitmap == null) {
            return null;
        }
        Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap, width, height, true);
        bitmap.recycle();
        return thumbBmp;
    }

    /**
     * url 转Bitmap
     *
     * @param url
     * @return
     */
    static Bitmap url2bitmap(String url) {
        Bitmap bitmap = null;
        try {
            URL imageurl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) imageurl.openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    static String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }
}
